#!/usr/bin/python -O


import collections
import os
#import scipy
import sys

sys.path.append('src/python/modules')

#from scipy.stats import normaltest
#from scipy.stats import f_oneway as anova

import utils
import wp_actives as wpa

SAMPLE_SIZE = 1000


def main(input_dir, output_dir):
    fields = ['Username', 'Cohort', 'Year', 'Offset', 'Lifespan', 'Current'] + utils.KITTUR_WORK_TYPES
    files = {}
    for m in wpa.METRICS:
        files[m] = open('%s/%s.txt' % (output_dir, m), 'w')
        files[m].write('\t'.join(fields) + '\n')
    for (is_current, udata, by_year) in wpa.WorkYearIterator(input_dir):
        for (year, year_metrics) in by_year.items():
            for m in wpa.METRICS:
                if year_metrics[m + '_total'] != 0:
                    tokens = [
                        udata[0]['username'],
                        udata[0]['cohort'],
                        year,
                        int(year) - udata[0]['cohort'],
                        udata[0]['lifespan'],
                        'TRUE' if is_current else 'FALSE'
                    ]
                    for t in utils.KITTUR_WORK_TYPES:
                        v = year_metrics[m + '_prop'].get(t.lower(), 0.0)
                        tokens.append(v)
                    files[m].write('\t'.join([str(t) for t in tokens]) + '\n')

    [f.close() for f in files.values()]

if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])
